package com.wing.cabinet.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.CellInventoryInout;
import com.wing.cabinet.model.entity.CellInventoryOutLabel;
import com.wing.cabinet.model.request.CellInventoryOutLabelForm;
import com.wing.cabinet.model.response.CellInventoryOutLabelVO;
import com.wing.cabinet.service.CellInventoryOutLabelService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-领新条码管理", tags = "#/cabinet/cellInventoryOutLabel")
@RestController("cellInventoryOutLabelController")
@RequestMapping("cabinet/admin/cellInventoryOutLabel")
@RequiredArgsConstructor
public class CellInventoryOutLabelController {

    private final CellInventoryOutLabelService cellInventoryOutLabelService;


    @ApiOperation(value = "查询所有领新条码", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CellInventoryOutLabelVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            , Long inoutDetailId
            , String startTime
            , String endTime
            , String skuName
            , String skuCode
            , String labelCode
            , String parentCode
            , String workOrderCode
            , String code
            , CellInventoryInout.BusinessType businessType
            , Long departmentId
            , Long roleId
            , String mainCabCode
            , String cabCode
            , String deviceCode4Query
            , Long creatorId
    ){
        Page<CellInventoryOutLabel> pg = new Page<>(page, limit);
        Map queryParam = new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        if(null != inoutDetailId){
            queryParam.put("inoutDetailId", inoutDetailId);
        }
        if(StringUtils.isNotBlank(startTime)){
            queryParam.put("startTime", startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            queryParam.put("endTime", endTime);
        }
        if(StringUtils.isNotBlank(skuName)){
            queryParam.put("skuName", skuName);
        }
        if(StringUtils.isNotBlank(skuCode)){
            queryParam.put("skuCode", skuCode);
        }
        if(StringUtils.isNotBlank(labelCode)){
            queryParam.put("labelCode", labelCode);
        }
        if(StringUtils.isNotBlank(parentCode)){
            queryParam.put("parentCode", parentCode);
        }
        if(StringUtils.isNotBlank(workOrderCode)){
            queryParam.put("workOrderCode", workOrderCode);
        }
        if(StringUtils.isNotBlank(code)){
            queryParam.put("code", code);
        }
        if(null != businessType){
            queryParam.put("businessType", businessType.getValue());
        }
        if(null != departmentId){
            queryParam.put("departmentId", departmentId);
        }
        if(null != roleId){
            queryParam.put("roleId", roleId);
        }
        if(StringUtils.isNotBlank(mainCabCode)){
            queryParam.put("mainCabCode", mainCabCode);
        }
        if(StringUtils.isNotBlank(cabCode)){
            queryParam.put("cabCode", cabCode);
        }
        if(StringUtils.isNotBlank(deviceCode4Query)){
            queryParam.put("deviceCode4Query", deviceCode4Query);
        }
        if(null != creatorId){
            queryParam.put("creatorId", creatorId);
        }
        IPage<CellInventoryOutLabelVO> list = cellInventoryOutLabelService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加领新条码", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(CellInventoryOutLabelForm.Add.class) CellInventoryOutLabelForm cellInventoryOutLabelForm) {
        if (cellInventoryOutLabelService.add(cellInventoryOutLabelForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改领新条码", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(CellInventoryOutLabelForm.Update.class) CellInventoryOutLabelForm cellInventoryOutLabelForm) {
        CellInventoryOutLabel cellInventoryOutLabel = cellInventoryOutLabelService.getById(cellInventoryOutLabelForm.getId());
        if(cellInventoryOutLabel == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CellInventoryOutLabel.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cellInventoryOutLabelForm, cellInventoryOutLabel, copyOptions);
        if (cellInventoryOutLabelService.updateById(cellInventoryOutLabel)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除领新条码", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cellInventoryOutLabelId", value = "领新条码id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long cellInventoryOutLabelId) {
        if (cellInventoryOutLabelService.removeById(cellInventoryOutLabelId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
